package com.mbc.rmt.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mbc.common.dao.DAO;
import com.mbc.rmt.entities.BranchBalance;

public class BranchBalanceDAOImpls extends DAO<BranchBalance> implements BranchBalanceDAO{
	
	public BranchBalanceDAOImpls(Connection conn) {
		this.conn = conn;
		this.table_name = "branchbalance";
	}
	
	@Override
	public void create(BranchBalance entity) throws SQLException {
		// TODO Auto-generated method stub
		query = "insert into BranchBalance "
				+ " ( branchbalance_id, branch_id, currency_id, openingbalance, currentbalance) values(?, ?, ?, ?, ?)";
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setLong(i++, entity.getBranchbalance_id());
		pstmt.setLong(i++, entity.getBranch_id());
		pstmt.setLong(i++, entity.getCurrencyid());
		pstmt.setDouble(i++, entity.getOpeningbalance());
		pstmt.setDouble(i++, entity.getCurrentbalance());
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void update(BranchBalance entity) throws SQLException {
		// TODO Auto-generated method stub
		query = "update "
				+ table_name
				+ " set branchbalance_id = ?, branch_id = ?, currency_id = ?, openingbalance= ?, currentbalance = ?"
				+ "where branchbalance_id = " + entity.getBranchbalance_id();
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setLong(i++, entity.getBranchbalance_id());
		pstmt.setLong(i++, entity.getBranch_id());
		pstmt.setLong(i++, entity.getCurrencyid());
		pstmt.setDouble(i++, entity.getOpeningbalance());
		pstmt.setDouble(i++, entity.getCurrentbalance());
		
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void delete(long key) throws SQLException {
		// TODO Auto-generated method stub
		query = getDeleteQuery() + " where branch_id = " + key;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public BranchBalance find(long key) throws SQLException {
		// TODO Auto-generated method stub
		BranchBalance l_Balance = null;
		query = getSelectQuery() + " where branch_id = " + key;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_Balance = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_Balance;
	}

	@Override
	public BranchBalance getInfo(BranchBalance info) throws SQLException {
		// TODO Auto-generated method stub
		BranchBalance l_Balance = null;
		query = getSelectQuery() + " where branch_id = " + info.getBranch_id() + " and "
				+ "currency_Id = " + info.getCurrencyid();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_Balance = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_Balance;
	}

	@Override
	protected BranchBalance readRecord(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		BranchBalance l_entity = new BranchBalance();
		l_entity.setBranchbalance_id(rs.getLong("branchbalance_id"));
		l_entity.setBranch_id(rs.getLong("branch_id"));
		l_entity.setCurrencyid(rs.getLong("currency_id"));
		l_entity.setOpeningbalance(rs.getDouble("openingbalance"));
		l_entity.setCurrentbalance(rs.getDouble("currentbalance"));
		return l_entity;
	}

}
